package cn.iune.erp.sale;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Aspect//指定这是一个其切面
public class SaleOrderDetailDaoAspect {
	
	@Autowired
	private ISaleService saleService;

	
	//指定切入点（增加）
    @Pointcut("execution(* cn.iune.erp.sale.ISaleOrderDetailDao.insert(..)) && args(obj)")
    public void insertPointCut(SaleOrderDetail obj){}
    //指定通知以及通知的范围
    @Transactional
    @After("insertPointCut(obj)")
    public void afterInsert(SaleOrderDetail obj){
//    	System.out.println("com.iune.erp.sale.ISaleOrderDetailDao.insert");
    	/*
		//处理关联单据（销售预测）
		if (obj.getOrigin()!=null 
			&& obj.getOrigin().equals("XSYC") 
			&& obj.getOriginNo()!=null 
			){
			//更新请购单的正在订购数量
			saleForecastDetailServer.updatePickItemQuantityIng(obj.getOriginNo(),obj.getEnterpriseNo(),obj.getItemAmount());
		}
		*/
    	saleService.afterInsertOrderDetail(obj);
    }
    
	//指定切入点（删除）
    @Pointcut("execution(* cn.iune.erp.sale.ISaleOrderDetailDao.delete(..)) && args(fID)")
    public void deletePointCut(String fID){}
    //指定通知以及通知的范围
    @Transactional
    @Before("deletePointCut(fID)")
    public void beforeDelete(String fID){
    	System.out.println("com.iune.erp.sale.ISaleOrderDetailDao.delete-->"+fID);
    	/*
		SaleOrderDetail obj = saleOrderDetailService.getByID(fID);
		//处理关联单据（销售预测）
		if (obj.getOrigin()!=null 
			&& obj.getOrigin().equals("XSYC") 
			&& obj.getOriginNo()!=null 
			){
			//更新请购单的正在订购数量
			saleForecastDetailServer.updatePickItemQuantityIng(obj.getOriginNo(),obj.getEnterpriseNo(),obj.getItemAmount().negate());
		}
		*/
    	saleService.beforeDeleteOrderDetail(fID);
    }
    
	//指定切入点（修改）
    @Pointcut("execution(* cn.iune.erp.sale.ISaleOrderDetailDao.update(..)) && args(obj)")
   public void updatePointCut(SaleOrderDetail obj){}
    //指定通知以及通知的范围
    @Transactional
    /*
    @Before("updatePointCut(obj)")
    public void beforeUpdate(SaleOrderDetail obj){
		//删除
		this.beforeDelete(obj.getfID());
		//插入
		this.afterInsert(obj);
   	
    }
    */
    @Around("updatePointCut(obj)")
     public SaleOrderDetail aroundUpdate(JoinPoint joinPoint, SaleOrderDetail obj) throws Throwable{
         System.out.println(this.getClass().getName()+"环绕通知上。。。-->"+obj.getSerialNo());
         saleService.beforeDeleteOrderDetail(obj.getfID());
         SaleOrderDetail objNew = (SaleOrderDetail) ((ProceedingJoinPoint) joinPoint).proceed();
         System.out.println(this.getClass().getName()+"环绕通知下。。。");
         saleService.afterInsertOrderDetail(obj);
         return objNew;
     }


}
